// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-06-18 17:06:48
// 生成路径: internal/app/app/logic/app_news_category.go
// 生成人：gfast
// desc:文章分类
// company:云南奇讯科技有限公司
// ==========================================================================

package appNewsCategory

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/app/dao"
	"github.com/tiger1103/gfast/v3/internal/app/app/model"
	"github.com/tiger1103/gfast/v3/internal/app/app/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/app/service"
	"github.com/tiger1103/gfast/v3/library/libUtils"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterAppNewsCategory(New())
}

func New() service.IAppNewsCategory {
	return &sAppNewsCategory{}
}

type sAppNewsCategory struct{}

func (s *sAppNewsCategory) List(ctx context.Context, req *model.AppNewsCategorySearchReq) (listRes *model.AppNewsCategorySearchRes, err error) {
	listRes = new(model.AppNewsCategorySearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.AppNewsCategory.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.AppNewsCategory.Columns().Id+" = ?", req.Id)
		}
		if req.Pid != "" {
			m = m.Where(dao.AppNewsCategory.Columns().Pid+" = ?", req.Pid)
		}
		if req.CateName != "" {
			m = m.Where(dao.AppNewsCategory.Columns().CateName+" like ?", "%"+req.CateName+"%")
		}
		order := "id asc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		// 查询总数
		listRes.Total, err = m.Count()
		// 查询列表
		err = m.Order(order).Scan(&listRes.List)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
	})
	return
}

func (s *sAppNewsCategory) GetById(ctx context.Context, id uint) (res *model.AppNewsCategoryInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.AppNewsCategory.Ctx(ctx).WithAll().Where(dao.AppNewsCategory.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
		if res != nil {
		}
	})
	return
}

func (s *sAppNewsCategory) Add(ctx context.Context, req *model.AppNewsCategoryAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppNewsCategory.Ctx(ctx).Insert(do.AppNewsCategory{
			CateName: req.CateName,
			Pid:      req.Pid,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sAppNewsCategory) Edit(ctx context.Context, req *model.AppNewsCategoryEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppNewsCategory.Ctx(ctx).WherePri(req.Id).Update(do.AppNewsCategory{
			CateName: req.CateName,
			Pid:      req.Pid,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sAppNewsCategory) Delete(ctx context.Context, ids []uint) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		ids, err = s.GetChildrenIds(ctx, ids)
		liberr.ErrIsNil(ctx, err)
		_, err = dao.AppNewsCategory.Ctx(ctx).Delete(dao.AppNewsCategory.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}

// GetChildrenIds 通过ID获取子级ID
func (s *sAppNewsCategory) GetChildrenIds(ctx context.Context, ids []uint) (returnIds []uint, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		//获取所有
		var all *model.AppNewsCategorySearchRes
		all, err = s.List(ctx, &model.AppNewsCategorySearchReq{})
		liberr.ErrIsNil(ctx, err)
		list := make(g.List, len(all.List))
		for k, info := range all.List {
			list[k] = gconv.Map(info)
		}
		for _, id := range ids {
			returnIds = append(returnIds, id)
			children := libUtils.FindSonByParentId(list, id, "pid", "id")
			for _, cid := range children {
				returnIds = append(returnIds, cid["id"].(uint))
			}
		}
	})
	return
}
